package com.ciwong.epaper.modules.evaluate.ui;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.evaluate.bean.SentenceAnswerBean;
import com.ciwong.epaper.modules.evaluate.bean.SentencesDetail;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.download.DownLoadInfo;

import java.util.ArrayList;

/**
 * 单独重读某个句子 Created by lzr on 2017/5/9.
 */
public class StressActivity extends SentenceRepeatActivity
{

    ArrayList<SentenceAnswerBean> answerList;

    int currIndex;

    int type;

    private SentenceAnswerBean answerBean;

    @Override
    protected void preCreate()
    {
        Intent intent = getIntent();
        sentencesDetails = (ArrayList<SentencesDetail>) intent
                .getSerializableExtra(IntentFlag.INTENT_FLAG_SENTENCES_LIST);
        type = intent.getIntExtra(IntentFlag.INTENT_FLAG_TYPE,
                SpeechPlayController.LISTENER_MODE_ZHUJU);
        currIndex = intent.getIntExtra(IntentFlag.INTENT_FLAG_SENT_INDEX, 0);
        position = intent.getIntExtra(IntentFlag.INTENT_FLAG_POSITION, 0);
        mModule = (Module) intent
                .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
        mDownLoadInfo = (DownLoadInfo) intent
                .getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
        mIsUnAutoMode = intent.getBooleanExtra(IntentFlag.INTENT_FLAG_BOOLEAN,
                false);
        if (mDownLoadInfo
                .getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW)
        {
            titleText = mDownLoadInfo.getResourceName();
        }
        else
        {
            titleText = mModule.getResourceList().get(position)
                    .getResourceName();
        }

    }

    @Override
    protected void findViews()
    {
        super.findViews();
        sumView.setVisibility(View.VISIBLE);
        speechView.setmIsReadingMode(mIsUnAutoMode);
        if (mIsUnAutoMode)
        {
            mIvCheckMode.setSelected(mIsUnAutoMode);
        }
        mRlCheckMode.setVisibility(View.GONE);

    }

    @Override
    protected void showDialog()
    {
        finish();
    }

    @Override
    protected void loadData()
    {

        initYzs();
        playAllView.setVisibility(View.GONE);
        speechPlayController.setStress(true);
        speechView.setStress(true);
        if (type != SpeechPlayController.LISTENER_MODE_XUANDUAN)
        {
            speechPlayController.setSentenceAnswers(answerList);
        }
        refreshSentencesUi(null);
        answerList = (ArrayList<SentenceAnswerBean>) getIntent()
                .getSerializableExtra(IntentFlag.INTENT_FLAG_SENT_AN);
        if (answerList.size() > 0)
        {
            answerBean = answerList.get(answerList.size() - 1);
        }
        speechPlayController.setSentenceAnswers(answerList);
        speechPlayController.setCurrentListenAndRepeatIndex(0);
        setAutoOrUnautoModle();
        handler.postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                playImg();
            }
        }, 400);
    }

    /**
     * 设置手动或者自动模式
     */
    protected void setAutoOrUnautoModle() {
        if (mIsUnAutoMode) {
            // 手动模式
            startImg.setVisibility(View.GONE);
            if (readMode == SpeechPlayController.LISTENER_MODE_ZHUJU) {
                play_iocn.setVisibility(View.VISIBLE);
            } else {
                play_iocn.setVisibility(View.GONE);
            }
            img_micro_recording.setVisibility(View.VISIBLE);
            play_dubing_iocn.setVisibility(View.GONE);
            playAllView.setVisibility(View.GONE);
            next_record.setVisibility(View.INVISIBLE);
            back_record.setVisibility(View.VISIBLE);
            stateView.setUnAutoRecordState(true);
        } else {
            // 自动模式
            startImg.setVisibility(View.VISIBLE);
            play_iocn.setVisibility(View.GONE);
            img_micro_recording.setVisibility(View.GONE);
            play_dubing_iocn.setVisibility(View.GONE);
            playAllView.setVisibility(View.VISIBLE);
            next_record.setVisibility(View.GONE);
            back_record.setVisibility(View.GONE);
            stateView.setUnAutoRecordState(false);
        }
        if (speechPlayController != null) {
            speechPlayController.setmIsUnAutoMode(mIsUnAutoMode);
        }
    }

    @Override
    protected void refreshSentencesUi(String saveUUid)
    {
        setNo(0, 1);
        showContent(saveUUid);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // refreshSpeechView(true);
    }

    @Override
    protected void onDestroy()
    {
        speechPlayController = null;
        super.onDestroy();
    }

    @Override
    protected void playImg()
    {
        if (speechPlayController
                .getCurrentListenAndRepeatIndex() >= (sentencesDetails.size()))
        {
            lastRecord(0, 0);
            return;
        }
        startPlay();
    }

    @Override
    protected boolean isNeedSaveAn()
    {
        return !super.isNeedSaveAn();
    }

    @Override
    public void lastRecord(int currentIndex, int count)
    {
        Intent intent = new Intent();
        intent.putExtra(IntentFlag.INTENT_FLAG_SENT_INDEX, currIndex);
        if (answerList.size() > 0 && answerBean != null)
        {
            answerList.get(answerList.size() - 1)
                    .setCountTime(answerBean.getCountTime());
            answerList.get(answerList.size() - 1)
                    .setReadTimes(answerBean.getReadTimes());
        }
        intent.putExtra(IntentFlag.INTENT_FLAG_SENT_AN, answerList);
        setResult(SentenceRepeatActivity.REQUEST_CODE_AN, intent);
        this.finish();
    }

    @Override
    public void stress(View v, RelativeLayout relativeLayout)
    {

    }

    @Override
    public void stressItemClick(View v, RelativeLayout relativeLayout)
    {

    }

    @Override
    public void stressIconRec(View v, RelativeLayout relativeLayout)
    {

    }

    @Override
    protected boolean isBack()
    {
        return !super.isBack();
    }
}
